Skip to main content

Adding Dependencies

Vite provides a modern and efficient way to manage third-party libraries in your project. Adding dependencies is simple and allows you to easily integrate various libraries and frameworks into your Vite project. In this guide, we’ll cover how to install and use third-party libraries like Lodash and Axios, and explain the benefits of using ES Modules (ESM) libraries for better compatibility with Vite.


1. Installing and Using Third-Party Libraries

To enhance your Vite project, you can integrate third-party libraries such as Lodash, Axios, or any other npm package. Vite works seamlessly with npm/yarn and automatically optimizes dependencies for the development environment.

Step-by-Step Guide

  1. Install the dependency: You can install any library from the npm registry by using either npm or yarn.

    • Using npm:

      npm install lodash axios
    • Using yarn:

      yarn add lodash axios
  2. Import and use the library: Once the dependency is installed, you can import it in your JavaScript files and use it directly. For example:

    • Using Lodash:

      import _ from 'lodash';

      const array = [1, 2, 3, 4, 5];
      const shuffledArray = _.shuffle(array);
      console.log(shuffledArray);
    • Using Axios:

      import axios from 'axios';

      axios.get('https://api.example.com/data')
      .then(response => {
      console.log(response.data);
      })
      .catch(error => {
      console.error('Error fetching data:', error);
      });
  3. Use the library in your application: After importing the library, you can use its methods and functionality throughout your app. Vite will automatically optimize and bundle the dependencies during the build process.

Example

Here’s an example where we install both Lodash and Axios, and use them in a simple JavaScript file:

# Install Lodash and Axios
npm install lodash axios
// main.js
import _ from 'lodash';
import axios from 'axios';

const array = [1, 2, 3, 4, 5];
const shuffledArray = _.shuffle(array);
console.log(shuffledArray);

axios.get('https://api.example.com/data')
.then(response => console.log(response.data))
.catch(error => console.error(error));

2. Using ESM Libraries for Better Compatibility with Vite

Vite is designed with native ES Modules (ESM) support. This means that libraries packaged as ESM are naturally compatible with Vite and benefit from optimizations like faster module resolution and improved tree-shaking.

Why Use ESM Libraries?

  • Faster build times: Since ESM libraries are already module-based, Vite can take advantage of modern browser features such as native module imports, which improves both development and build time performance.
  • Better tree-shaking: ESM allows for more efficient tree-shaking, which means that unused code can be eliminated during the build process, reducing your bundle size.
  • Native support: ESM is supported natively by modern browsers and Vite, making it the preferred choice for modern web development.

Example of Using an ESM-Compatible Library

Let’s consider using a library like date-fns, which is designed to be used with ESM. You can install it via npm:

npm install date-fns

Then, import and use it in your project:

import { format } from 'date-fns';

const formattedDate = format(new Date(), 'yyyy-MM-dd');
console.log(formattedDate);

In this case, since date-fns is an ESM-compatible library, Vite will optimize it automatically, and you can enjoy faster builds and better performance.

Benefits of ESM-Compatible Libraries

  • Smaller Bundles: With tree-shaking and proper import mechanisms, only the parts of the library that you use will be bundled.
  • Improved Compatibility: Libraries that are ESM-based are fully compatible with Vite, as Vite itself is built around ESM.
  • Simpler Configuration: You don't need additional tools or configurations to use ESM libraries with Vite, as it supports them out-of-the-box.

3. Dealing with Non-ESM Libraries

While Vite works perfectly with ESM libraries, you might encounter some older or non-ESM libraries that require special handling. In these cases, Vite can still handle them, but you may need to use the optimizeDeps feature or an external CDN.

Example: Using a Non-ESM Library (e.g., jQuery)

If you are using a non-ESM library like jQuery, you may need to instruct Vite to optimize it:

  1. Install the library:

    npm install jquery
  2. Modify your vite.config.js to tell Vite to optimize the library:

    // vite.config.js
    export default {
    optimizeDeps: {
    include: ['jquery']
    }
    };
  3. Use the library in your project:

    import $ from 'jquery';

    $(document).ready(function() {
    $('body').append('<h1>Hello, Vite!</h1>');
    });

Example: Using a CDN

Alternatively, you can include the library directly from a CDN by adding it to your index.html file:

<!-- index.html -->
<script src="https://cdn.jsdelivr.net/npm/jquery"></script>

This approach works well for small or external dependencies that you don't want to install directly via npm.


4. Conclusion

Adding dependencies to your Vite project is a simple process, whether you’re using popular libraries like Lodash or Axios, or working with ESM-based libraries for better performance. Vite makes integrating third-party libraries easy, thanks to its fast development environment and built-in optimizations like tree-shaking.

When possible, it’s recommended to use ESM-compatible libraries to take full advantage of Vite’s performance benefits. However, for older or non-ESM libraries, Vite provides configurations to ensure compatibility, whether by optimizing dependencies or leveraging CDN-hosted versions.

By carefully choosing your dependencies and understanding how to best work with them in the Vite ecosystem, you can create faster, more efficient applications with less effort.